package com.srs.dao.agent;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.agent.NoCallGenerated;
import com.srs.util.HibernateUtil;

public class NoCallGeneratedDao {

	final static Logger logger = LoggerFactory.getLogger(NoCallGeneratedDao.class);

	private List<NoCallGenerated> noCallGeneratedList;
	private NoCallGenerated noCallGenerated;

	public void delete(Integer id) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			noCallGenerated = (NoCallGenerated) session.get(NoCallGenerated.class, id);
			session.delete(noCallGenerated);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<NoCallGenerated> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			noCallGeneratedList = session.createQuery("from User").list();
			return noCallGeneratedList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<NoCallGenerated> list(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from NoCallGenerated nc where nc.onDate=:onDate");
			q.setString("onDate", onDate);
			noCallGeneratedList = q.list();
			return noCallGeneratedList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}			
	
	@SuppressWarnings("unchecked")
	public List<Integer> listAgentIds(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("select nc.agentId from NoCallGenerated nc where nc.onDate=:onDate");
			q.setString("onDate", onDate);
			List<Integer> listAgentIds = q.list();
			return listAgentIds;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}
	}	

	@SuppressWarnings("unchecked")
	public List<Integer> listAgentIds() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("select nc.agentId from NoCallGenerated  nc");
			List<Integer> listAgentIds = q.list();
			return listAgentIds;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}
	}	

	public Long count(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("select count(nc.agentId) from NoCallGenerated nc where nc.onDate=:onDate");
			q.setString("onDate", onDate);
			return (Long) q.uniqueResult();
		} finally {
			session.close();
		}

	}	
	
	public Long count(String onDate, String agentList) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("select count(distinct nc.agentId) from NoCallGenerated nc where nc.onDate=:onDate and nc.agentId in " + agentList);
			q.setString("onDate", onDate);
			return (Long) q.uniqueResult();
		} finally {
			session.close();
		}

	}	
		
	public NoCallGenerated get(Integer id) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.uid=:id");
			q.setInteger("userid", id);
			return (NoCallGenerated) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	
	public void insert(NoCallGenerated usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(NoCallGenerated usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
